<?php
if(!$is_success){
    $dp = new jDatePicker();
    $dp->__dateFormat = "yy-mm-dd";
    echo "<scr"."ipt type=\"text/javascript\">";
    $dp->generateDP("tanggal_dn");
    echo "</scr"."ipt>";
    $dp->init();
?>
<script>    
        
    function append_table(result){
        //var result = jQuery.parseJSON(array);
        var length = jQuery("#tablebody").find("tr").length;
        
        if(length == 1){
            jQuery("#no_item").hide();
        }
          
        var index = -1, i = -1;
        var $ids = jQuery("input[name='id_item[]']");
        if($ids.length > 0){
            $ids.each(function(){
                if(this.value != result.item)
                    i++;
                else{
                    index = i + 1;
                    
                    var $qty = jQuery("input[name='quantity[]']").eq(index);
                    var $td = jQuery("#tablebody").find(".GridItem1").eq(index + 1).find("td");
                    var new_qty = parseInt(result.qty) + parseInt($qty.val());
                    
                    $qty.val(new_qty);
                    $td.eq(2).html(new_qty + ' ' + result.satuan);
                }
            });
        }
           
        if(index == -1)
            jQuery("#tablebody").append(
                '<tr class="GridItem1">\n'+
                    '<td>'+length+'<input type="hidden" name="quantity[]" value="'+result.qty+'" /><input type="hidden" name="id_item[]" value="'+result.item+'" /></td>\n'+
                    '<td>'+result.nama+'</td>\n'+
                    '<td>'+result.qty+' '+result.satuan+'</td>\n'+
                    '<td>&nbsp;</td>\n'+
                    '<td>&nbsp;</td>\n'+
                    '<td><a class="delete" onclick="remove_element(this);">&nbsp;</a></td>'+
                '</tr>'
            );
    } 
    
    function remove_element(elm){
        var tr_parent = jQuery(elm).parent().parent();
        jQuery(tr_parent).remove();
        
        var length = jQuery("#tablebody").find("tr").length;
        
        if(length == 1){
            jQuery("#no_item").show();
        }
    }
        
    function setBlnToRome(bln){
        if(bln=="01"){
            return "I";
        }else if(bln=="02"){
            return "II";
        }else if(bln=="03"){
            return "III";
        }else if(bln=="04"){
            return "IV";
        }else if(bln=="05"){
            return "V";
        }else if(bln=="06"){
            return "VI";
        }else if(bln=="07"){
            return "VII";
        }else if(bln=="08"){
            return "VIII";
        }else if(bln=="09"){
            return "IX";
        }else if(bln=="10"){
            return "X";
        }else if(bln=="11"){
            return "XI";
        }else{
            return "XII";
        }
    }
    
    function setDNNumber(){
         var prefix = <?php echo json_encode($prefix); ?>;

         var date = jQuery("#tanggal_dn").val();
         var partsArray = date.split('-');
         var tgl = partsArray[2];
         var bln = setBlnToRome(partsArray[1]);
         var thn =  partsArray[0];
         var no= prefix + "/ASP/DN/" + tgl +"/" + bln +"/" +thn;
         jQuery("#nomor_dn").val(no);
    }
   
    function close_gb(){
        <?php echo CloseGB(false); // close greybox   ?>
    }
    
    function _obj_fSendValue(kode) {
        location.href = '<?php echo site_url("transaksi/delivery_add/$kode") ?>?quot=' + kode;
    }
</script>

<h3 class="title-popup">Form Tambah DN</h3>

<div style="padding:0 10px 0 10px;">
    <?php if ($error != "") { ?>
        <div class="BoxMessage Warning">Proses Gagal, <br /><?php echo $error; ?></div>
    <?php } ?>
    
<div id="content">
<div class="DivTable">
<?= form_open(site_url("transaksi/delivery_add/$kode/$id_invoice")) ?>
<table>
    <tr>
        <td width="5%">&nbsp;</td>
        <td width="1%">&nbsp;</td>
        <td width="40%">&nbsp;</td>
        <td width="8%">&nbsp;</td>
        <td width="5%">&nbsp;</td>
        <td width="1%">&nbsp;</td>
        <td width="35%">&nbsp;</td>
        <td width="5%">&nbsp;</td>
    </tr>
    <tr>
        <td>DN NO</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "nomor_dn", "id" => "nomor_dn", "size" => "50"),$nomor_dn);?></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Date</td>
        <td>:</td>
        <td>
            <?php 
                echo form_input(array("name" => "tanggal_dn", "id" => "tanggal_dn" ,"class" => "read-only", "readonly" => "readonly", "size" => "47"), $tanggal_dn,"onchange=\"setDNNumber()\"");
                if($nomor_dn == "")
                    echo "<script>setDNNumber();</script>";
            ?>
        </td>
    </tr>
    <tr>
        <td>To</td>
        <td>:</td>
        <td>
            <?php echo form_input(array("name" => "to", "id" => "to", "class" => "input_text", "size" => "50"),$to);?>
        </td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>RF No.</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "rf_number", "id" => "rf_number", "size" => "50"),$rf_number);?></td> 
    </tr>
    <tr>
        <td>Delivery To</td>
        <td>:</td>
        <td>
            <?php echo form_input(array("name" => "delivery_to", "id" => "delivery_to", "class" => "input_text", "size" => "50"),$delivery_to);?>
        </td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Subject</td>
        <td>:</td>
        <td>
            <?php echo form_input(array("name" => "subject", "id" => "subject", "class" => "input_text", "size" => "50"),$subject);?>
        </td>
    </tr>
    <tr>
        <td>Location</td>
        <td>:</td>
        <td>
            <?php echo form_input(array("name" => "location", "id" => "location", "class" => "input_text", "size" => "50"),$location);?>
        </td>
        <td colspan="4">&nbsp;&nbsp;&nbsp;</td>
    </tr>

    <tr>
        <td colspan="7">
            <br/>
            <?php echo $gbitem?>
            <br/><br/>

            <table class="GridTable" style="width: 100%;">
                <thead>
                    <tr class="GridHeader">
                        <td>No.</td>
                        <td>Description</td>
                        <td>Qty</td>
                        <td>Check Delivery</td>
                        <td>Check Vessel</td>
                        <td>Action</td>
                    </tr>
                </thead>
                <tbody id="tablebody">
                    <tr class="GridItem1" id="no_item">
                        <td colspan="8">Tidak ada data</td>
                    </tr>
                    <?php 
                        if(is_object($item)){
                        if($item->num_rows() > 0) {
                            $nomor = 1;
                            foreach($item->result() as $row){
                    ?>
                            <tr class="GridItem1">
                                <td><?php echo $nomor++;?></td>
                                <td><input type="hidden" name="quantity[]" value="<?php echo $row->quantity?>" /><?php echo $row->nama_item?><input type="hidden" name="id_item[]" value="<?php echo $row->id_item?>" /></td>
                                <td><?php echo $row->quantity . " " . $row->satuan_beli?></td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td><a class="delete" onclick="remove_element(this);">&nbsp;</a></td>
                            </tr>
                    <?php 
                            }
                            
                            echo "<script>jQuery(\"#no_item\").hide();</script>";
                        } 
                        }
                    ?>
                </tbody>
            </table> 
            <br/><br/>
        </td>
    </tr>  
    <tr>    
        <td>Checked By</td>
        <td>:</td>
        <td><?php if ($checked_by == "") $checked_by = "...................."; echo form_input(array("name" => "checked_by", "id" => "checked_by", "size"=>"50"), $checked_by);?></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
        <td>Delivered By</td>
        <td>:</td>
        <td><?php if ($delivered_by == "") $delivered_by = "...................."; echo form_input(array("name" => "delivered_by", "id" => "delivered_by" ,"class" => "input_text", "size"=>"50"), $delivered_by);?></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr> 
    <tr>
        <td>Received By</td>
        <td>:</td>
        <td><?php if ($received_by == "") $received_by = "...................."; echo form_input(array("name" => "received_by", "id" => "received_by" ,"class" => "input_text", "size"=>"50"), $received_by);?></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
        <td>Received By</td>
        <td>:</td>
        <td><?php if ($received_by_2 == "") $received_by_2 = "...................."; echo form_input(array("name" => "received_by_2", "id" => "received_by_2" ,"class" => "input_text", "size"=>"50"), $received_by_2);?></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr>    
</table>
<br class="bersih" />
<div id="box-control-save">
    <?=form_submit(array("name" => "btnSubmit", "class"=>"CommandButton"), "Save")?>&nbsp;
    <?=form_button(array("class"=>"CommandButton"), "Cancel", "onclick='close_gb();'")?>
</div>
<?=form_close()?>

</div>
</div>
</div>

<?php } else { ?>
<script type='text/javascript'>
    function close_gb(){
        <?php echo CloseGB(true); // close greybox   ?>
    }
</script>

<div style="padding-top: 25%;">
<div class="BoxMessage">Proses Berhasil, apakah anda mau mencetak delivery note tersebut?<br /><?php echo $error; ?></div>

<div id="box-control-save">
    <?=form_button(array("class"=>"CommandButton"), "Ya", "onclick=\"location.href='".site_url("transaksi/delivery_print/$kode")."';\"")?>&nbsp;
    <?=form_button(array("class"=>"CommandButton"), "Tidak", "onclick='close_gb();'")?>
</div>
</div>
<?php } ?>

